/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models;

import java.sql.*;
import java.util.Vector;

import Beans.NgonNgu;
import Beans.KhuVuc;

/**
 *
 * @author VIC
 */
public class KhuVuc_Model {
    /* Hàm Tạo mã cho khu vực kế tiếp được lập
     * trả về : mã cho khu vực kế tiếp.
     */

    public static int TaoMa() {
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "select max(makv) from khuvuc";
            rss = stmt.executeQuery(query);
            int maht = 0;// biến maht chứa mã của khu vực hiện tại
            while (rss.next()) {
                maht = rss.getInt(1);
            }
            stmt.close();
            rss.close();
            conn.close();
            return maht + 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }
    /* Hàm Lập Khu Vực thực hiện lập khu vực mới
     * tham số : mã ngôn ngữ, tên khu vực theo ngôn ngữ đó.
     * trả về : true nếu lập thành công, false nếu không lập được
     */

    public static boolean LapKhuVuc(int makv, String tenkv) // Tạo khu vực
    {
        Connection conn = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();

            NgonNgu[] dsnn = NgonNgu_Model.DSNgonNgu();
            String x = "insert into khuvuc values ";
            for (int i = 0; i < dsnn.length; i++) {
                x = x + "(" + makv + ", " + dsnn[i].getMaNN() + ", '" + tenkv + "', 1),";
            }
            String query = x.substring(0, x.length() - 1);
            int n = stmt.executeUpdate(query);
            stmt.close();
            conn.close();
            if (n == 0) {
                return false;// thất bại
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public static boolean KiemTraTenKV(String tenkv) {

        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "select * from khuvuc where TenKV = '" + tenkv + "'";
            rss = stmt.executeQuery(query);
            boolean n = false;
            if (rss.next()) {
                n = true;
            }
            stmt.close();
            rss.close();
            conn.close();
            return n;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public static boolean KiemTraSuaTenKV(String tenkv, String makv) {
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "select * from khuvuc where TenKV = '" + tenkv + "' and MaKV != " + makv;
            rss = stmt.executeQuery(query);
            boolean n = false;
            if (rss.next()) {
                n = true;
            }
            stmt.close();
            rss.close();
            conn.close();
            return n;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public Vector LayKV(int makv) {
        Vector v = new Vector();
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "select * from khuvuc where MaKV = " + makv;
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                Beans.KhuVuc kv = new Beans.KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setMaNN(rss.getInt(2));
                kv.setTenKV(rss.getString(3));
                kv.setTinhTrang(rss.getByte(4));
                v.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return v;
    }

    public static boolean Edit(String makv, String mann, String tenkv) {
        Connection conn = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "Update khuvuc set TenKV = '" + tenkv + "' where MaKV = " + makv + " and MaNN = " + mann;
            int n = stmt.executeUpdate(query);
            stmt.close();
            conn.close();
            if (n == 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public static boolean UpdateStatusKV(String makv, String status) {
        Connection conn = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "Update khuvuc set TinhTrang = " + status + " where MaKV = " + makv;
            int n = stmt.executeUpdate(query);
            stmt.close();
            conn.close();
            if (n == 0) {
                return false;// thất bại
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public Vector LayDSKV(int langid) {

        Vector v = new Vector();
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + "  ";
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                Beans.KhuVuc kv = new Beans.KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setMaNN(rss.getInt(2));
                kv.setTenKV(rss.getString(3));
                kv.setTinhTrang(rss.getByte(4));
                v.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return v;
    }

    public Vector LayDSKVTaoBan(int langid) {

        Vector v = new Vector();
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " and TinhTrang = 1 ";
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                Beans.KhuVuc kv = new Beans.KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setMaNN(rss.getInt(2));
                kv.setTenKV(rss.getString(3));
                kv.setTinhTrang(rss.getByte(4));
                v.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return v;
    }

    public static int LayKVTaoBan(int langid) {
        int makv = 0;
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "select MaKV from khuvuc where MaNN = " + langid + " and TinhTrang = 1 limit 1 ";
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                makv = rss.getInt(1);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return makv;
    }

    public Vector LayDSKVDB(int MaKV, int langid) {

        Vector v = new Vector();
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " and MaKV != " + MaKV + "  ";
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                Beans.KhuVuc kv = new Beans.KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setMaNN(rss.getInt(2));
                kv.setTenKV(rss.getString(3));
                kv.setTinhTrang(rss.getByte(4));
                v.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return v;
    }

    public Vector LayDSKVPPV(int langid) {

        Vector v = new Vector();
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " and TinhTrang = 1 ";
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                Beans.KhuVuc kv = new Beans.KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setMaNN(rss.getInt(2));
                kv.setTenKV(rss.getString(3));
                kv.setTinhTrang(rss.getByte(4));
                v.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return v;
    }

    public Vector LayDSKVTao(int langid) {

        Vector v = new Vector();
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();

            String query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " Order by MaKV desc ";

            rss = stmt.executeQuery(query);
            while (rss.next()) {
                Beans.KhuVuc kv = new Beans.KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setMaNN(rss.getInt(2));
                kv.setTenKV(rss.getString(3));
                kv.setTinhTrang(rss.getByte(4));
                v.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return v;
    }

    public Vector SortTenKV(int langid, String order) {
        Vector v = new Vector();
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " Order by TenKV " + order;

            rss = stmt.executeQuery(query);
            while (rss.next()) {
                Beans.KhuVuc kv = new Beans.KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setMaNN(rss.getInt(2));
                kv.setTenKV(rss.getString(3));
                kv.setTinhTrang(rss.getByte(4));
                v.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return v;
    }

    public Vector SortTTKV(int langid, String order) {

        Vector v = new Vector();
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();

            String query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " Order by TinhTrang " + order;
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                Beans.KhuVuc kv = new Beans.KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setMaNN(rss.getInt(2));
                kv.setTenKV(rss.getString(3));
                kv.setTinhTrang(rss.getByte(4));
                v.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return v;
    }

    public Vector SortSearchTenKV(int langid, String order, String tenkv, String status) {

        Vector v = new Vector();
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();

            String query = "";
            if (!tenkv.equals("") && status.equals("all")) {
                query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " and TenKV like '%" + tenkv + "%' Order by TenKV " + order;
            } else if (tenkv.equals("") && !status.equals("all")) {
                query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " and TinhTrang = " + status + " Order by TenKV " + order;
            } else if (tenkv.equals("") && status.equals("all")) {
                query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " Order by TenKV " + order;
            } else {
                query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " and TinhTrang = " + status + " and TenKV like '%" + tenkv + "%' Order by TenKV " + order;
            }
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                Beans.KhuVuc kv = new Beans.KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setMaNN(rss.getInt(2));
                kv.setTenKV(rss.getString(3));
                kv.setTinhTrang(rss.getByte(4));
                v.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return v;
    }

    public Vector SortSearchTTKV(int langid, String order, String tenkv, String status) {

        Vector v = new Vector();
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();

            String query = "";
            if (!tenkv.equals("") && status.equals("all")) {
                query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " and TenKV like '%" + tenkv + "%' Order by TinhTrang " + order;
            } else if (tenkv.equals("") && !status.equals("all")) {
                query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " and TinhTrang = " + status + " Order by TinhTrang " + order;
            } else if (tenkv.equals("") && status.equals("all")) {
                query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + "  Order by TinhTrang " + order;
            } else {
                query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " and TinhTrang = " + status + " and TenKV like '%" + tenkv + "%' Order by TinhTrang " + order;
            }

            rss = stmt.executeQuery(query);
            while (rss.next()) {
                Beans.KhuVuc kv = new Beans.KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setMaNN(rss.getInt(2));
                kv.setTenKV(rss.getString(3));
                kv.setTinhTrang(rss.getByte(4));
                v.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return v;
    }

    public Vector SearchKV(String tenkv, String status, int langid) {

        Vector v = new Vector();
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "";
            if (!tenkv.equals("") && status.equals("all")) {
                query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " and TenKV like '%" + tenkv + "%' Order by MaKV desc";
            } else if (tenkv.equals("") && !status.equals("all")) {
                query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " and TinhTrang = " + status + " Order by MaKV desc";
            } else if (tenkv.equals("") && status.equals("all")) {
                query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " Order by MaKV desc";
            } else {
                query = "select distinct(MaKV), MaNN, TenKV, TinhTrang from khuvuc where MaNN = " + langid + " and TinhTrang = " + status + " and TenKV like '%" + tenkv + "%' Order by MaKV desc";
            }

            rss = stmt.executeQuery(query);
            while (rss.next()) {
                Beans.KhuVuc kv = new Beans.KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setMaNN(rss.getInt(2));
                kv.setTenKV(rss.getString(3));
                kv.setTinhTrang(rss.getByte(4));
                v.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
        } catch (Exception e) {
            e.getMessage();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return v;
    }

    public static int Test() {
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = "select * from khuvuc";
            rss = stmt.executeQuery(query);
            int maht = 0;// biến maht chứa mã của khu vực hiện tại
            while (rss.next()) {
                maht = maht + 1;
            }
            stmt.close();
            rss.close();
            conn.close();
            return maht;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public static KhuVuc[] DSKhuVuc(int makv, int langid) {

        Vector dsnn = new Vector();// biáº¿n dsnn lÆ°u danh sÃ¡ch ngÃ´n ngá»¯ hiá»‡n cÃ³.
        String query = "";
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            query = "Select * from khuvuc where MaKV != " + makv + " and MaNN = " + langid;
            rss = stmt.executeQuery(query);
            //rss = ConnectionPool.Select(query);
            while (rss.next()) {
                Beans.KhuVuc kv = new Beans.KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setMaNN(rss.getInt(2));
                kv.setTenKV(rss.getString(3));
                kv.setTinhTrang(rss.getByte(4));
                dsnn.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
            return convertToArray(dsnn);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public static KhuVuc[] convertToArray(Vector v) {
        int n = v.size();
        KhuVuc[] arr = new KhuVuc[n];
        for (int i = 0; i < n; i++) {
            arr[i] = (KhuVuc) v.get(i);
        }
        return arr;
    }

    public static int LayMaKV(int langid) {
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            String query = " select MaKV from khuvuc where MaNN = " + langid + " limit 1 ";
            rss = stmt.executeQuery(query);
            int maht = 0;// biến maht chứa mã của khu vực hiện tại
            while (rss.next()) {
                maht = rss.getInt(1);
            }
            stmt.close();
            rss.close();
            conn.close();
            return maht;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }
    public static KhuVuc[] DSKV(int langid) {

        Vector vdstso = new Vector();
        String query = "";
        Connection conn = null;
        ResultSet rss = null;
        Statement stmt = null;
        try {
            ConnectionPool pool = new ConnectionPool();
            conn = pool.getConnection();
            stmt = conn.createStatement();
            query = " Select * from khuvuc where mann = " + langid + " ";
            rss = stmt.executeQuery(query);
            while (rss.next()) {
                KhuVuc kv = new KhuVuc();
                kv.setMaKV(rss.getInt(1));
                kv.setTenKV(rss.getString(3));
                vdstso.add(kv);
            }
            stmt.close();
            rss.close();
            conn.close();
            return convertToArray(vdstso);

        } catch (Exception e) {
            return null;
        }
        finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                }
            }
            if (rss != null) {
                try {
                    rss.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }

}
